//解析地址
dictdata['area'] = AreaListData;
//隐藏弹窗
$('#alert').hide();
//恢复滚动条
$('body').css({"overflow-y": "auto"});
//轮播图
$(function () {
    var firstLi = $('#play ul>li').eq(0).clone();
    var liLen = $('#play ul li').length;
    var i = 0;
    var timer;
    $('#play ul').append(firstLi);
    function play(index) {
        $('#play ul').stop().animate({'margin-left': -650 * index}, 500);
        if (index > (liLen - 1)) {
            index = 0;
        }
        $('#play ol>li').eq(index).addClass('sl').siblings().removeClass('sl');
    }
    $('#right').click(function () {
        i++;
        if (i == (liLen + 1)) {
            $('ul').stop().css({'margin-left': 0});
            i = 1;
        }
        play(i);
    });
    $('#left').click(function () {
        i--;
        if (i < 0) {
            $('ul').stop().css({'margin-left': -650 * liLen});
            i = liLen - 1;
        }
        play(i);
    });
    function dsq() {
        timer = setInterval(function () {
            $('#right').click();
        }, 3000);
    }
    dsq();
    $('#play').hover(function () {
        clearInterval(timer);
    }, function () {
        dsq();
    });
    $('#play ol>li').mouseover(function () {
        i = $(this).index();
        play(i);
    });
});

//最新发布类别颜色
$('[projectType]').each(function () {
    if ($(this).attr('projectType') == 2) {
        var url = $(this).next().attr("href");
        $(this).css('background', '#009688').next().attr("href", "/source/details" + url.substr(url.lastIndexOf("/")));
    }
});

//格式化采购项目截止时间
$('[endtime]').each(function () {
    var endtime = Math.ceil($(this).attr('endtime') * 1000);
    var currentDate = new Date().getTime();
    var day = Math.ceil((endtime - currentDate) / 86400000);
    $(this).text("还剩" + day + "天");
    if (day == 0 || day + 7 >= 0) {
        $(this).text("今天");
    }
    if (day + 7 < 0) {
        $(this).text("已截止").parents('tr').find('a').eq(0).attr('href', "");
        $(this).next().text("已截止");
    }
    if (endtime == 2769091200000) {
        $(this).text("长期有效").parents('tr').find('a').eq(0).attr('href', '/purchase/result/' + $(this).attr('id'));
        $(this).next().text("长期有效");
    }
});

//优秀供应商排行样式
$('#excellent ul li').eq(1).find("i").css('background', '#abad18');
$('#excellent ul li').eq(2).find("i").css('background', '#4aa20a');

//格式化地址
dictdata['area'] = AreaListData;


//登陆后显示个人中心入口
if (account != undefined && account != false) {
    $('#login,#register').hide();
    $('#personal p').html('用户' + account + '<br/>欢迎来到企企通').parents('#personal').show();
}

//采购项目点击换一换
var num = Math.ceil($('#numb').val() / 10);
var pag = 0;
var txt = "";
$('#switch span').click(function () {
    if (pag < num) {
        pag++;
    } else {
        pag = 1;
    }
    $('tbody').empty();
    txt = "";
    new Ajax("/index/change").keyvalue("pag", pag).getJson(function (json) {
        $.each(json, function (key, val) {
            var endtime = Math.ceil(val["enddt"] * 1000);
            var state = val["state"];
            var currentDate = new Date().getTime();
            var day = Math.ceil((endtime - currentDate) / 86400000);
            var residue = "还剩" + day + "天";
            var typ = "";
            var href;
            var href1;
            if (day == 0 || day + 7 >= 0) {
                residue = "今天";
                href = "/purchase/details/" + val["projectid"] + "";
                typ = "立即报价";
                href1= "/purchase/details/" + val["projectid"] + "";
            }
            if (day + 7 < 0||state=="2") {
                residue = "已截止";
                href = "";
                typ = "已截止";
                href1="/purchase/result/" + val["projectid"] + "";
            }
            txt += "<tr><td><a href=" + href1 + " target='_blank''>" + val["name"] + "</a></td><td>" + val["materielname"] + "</td><td>" + residue + "</td><td><a href=" + href + ">" + typ + "</a></td></tr>";
        })
        $('tbody').html(txt);
    })
});
//点击退出
$('#top div em').click(function () {
    $('#personal').hide();
    $('#login,#register').show();
});
//判断是否被冻结
//$('#publish').find('a').click(function(){
//    
//})
if(Cookie.Get("myid")){
new Ajax("/api/user/status").keyvalue("myid", Cookie.Get("myid")).getJson(function (json) {
    var status = json[0].status;
    var endtime = parseInt(json[0].endtime);
    var timestamp =parseInt(Date.parse(new Date()) / 1000);
    var pub = $('#publish').find('a');
    var pub1 = $('#publish').find('a').eq(0);
    var pub2 = $('#publish').find('a').eq(1);
    if (status == 0 || endtime <= timestamp) {
        new Ajax("/api/user/changestatus").keyvalue("myid",Cookie.Get("myid")).get(function(){
        });
        pub.attr("href", "javascript:void(0);");
        pub.attr("target", "");
        pub.click(function () {
            $('#sele').show();
        })
    }else{
        pub1.attr("href", "/source/create");
        pub1.attr("target", "_blank");
        pub2.attr("href", "/purchase/create");
        pub2.attr("target", "_blank");
    }
});
}

$('#confirm').click(function () {
    $('#sele').hide();
})